<script setup lang="ts">
import { ref, reactive } from "vue";
import type { FormRules, UploadProps } from "element-plus";
import ReCol from "@/components/ReCol";
import { FormProps } from "./types";
import { usePublicHooks } from "@/views/hooks";
import feedback from "@/utils/feedback";
import httpConfig from "@/utils/http/httpConfig";
import { Plus } from "@element-plus/icons-vue";
import axios from "axios";

import SingleUpload from "@/components/ReUpload/singleUpload.vue";

const props = withDefaults(defineProps<FormProps>(), {
  formInline: () => ({
    higherOptions: [], //状态其他数据,
    dictOptionData: {}, //字典数据
    id: null,

    name: null,

    logo: null,
    cid: null,
    showStatus: 0,

    firstLetter: null,

    sort: null,

    revision: null,

    createBy: null,

    createTime: null,

    updateBy: null,

    updateTime: null,

    remark: null,

    delFlag: null,

    tenantId: null
  })
});

const ruleFormRef = ref();
const { switchStyle } = usePublicHooks();
const newFormInline = ref(props.formInline);

function getRef() {
  return ruleFormRef.value;
}
//校验规则
const formRules = reactive(<FormRules>{
  name: [{ required: true, message: "品牌名为必填项", trigger: "blur" }],
  logo: [],
  showStatus: [
    {
      required: true,
      message: "显示状态为必填项",
      trigger: "blur"
    }
  ],
  firstLetter: [
    { required: true, message: "检索首字母为必填项", trigger: "blur" }
  ],
  sort: [],
  remark: []
});
defineExpose({ getRef });
</script>

<template>
  <el-form
    ref="ruleFormRef"
    :model="newFormInline"
    :rules="formRules"
    label-width="82px"
  >
    <el-row :gutter="30">
      <re-col :value="12" :xs="24" :sm="24">
        <el-form-item label="分类" prop="cid">
          <el-cascader
            v-model="newFormInline.cid"
            class="w-full"
            :options="newFormInline.dictOptionData.categotyList"
            :props="{
              value: 'id',
              label: 'name',
              emitPath: false,
              checkStrictly: false
            }"
            clearable
            filterable
            placeholder="请选择上级分类"
          >
            <template #default="{ node, data }">
              <span>{{ data.name }}</span>
              <span v-if="!node.isLeaf"> ({{ data.children.length }}) </span>
            </template>
          </el-cascader>
        </el-form-item>
        <el-form-item label="品牌名" prop="name">
          <el-input
            v-model="newFormInline.name"
            clearable
            placeholder="请输入品牌名"
          />
        </el-form-item>

        <el-form-item label="图片" prop="logo">
          <single-upload v-model="newFormInline.logo"></single-upload>
        </el-form-item>

        <el-form-item label="显示状态" prop="showStatus">
          <el-switch
            v-model="newFormInline.showStatus"
            inline-prompt
            :active-value="0"
            :inactive-value="1"
            active-text="启用"
            inactive-text="停用"
            :style="switchStyle"
          />
        </el-form-item>

        <el-form-item label="检索首字母" prop="firstLetter">
          <el-input
            v-model="newFormInline.firstLetter"
            clearable
            placeholder="请输入检索首字母"
          />
        </el-form-item>

        <el-form-item label="排序" prop="sort">
          <el-input
            v-model="newFormInline.sort"
            clearable
            placeholder="请输入排序"
          />
        </el-form-item>

        <el-form-item label="备注" prop="remark">
          <el-input
            v-model="newFormInline.remark"
            type="textarea"
            :autosize="{ minRows: 4, maxRows: 6 }"
            clearable
            placeholder="请输入备注"
            maxlength="200"
            show-word-limit
          />
        </el-form-item>
      </re-col>
    </el-row>
  </el-form>
</template>
<style scoped>
.avatar-uploader .avatar {
  width: 178px;
  height: 178px;
  display: block;
}
</style>

<style>
.avatar-uploader .el-upload {
  border: 1px dashed var(--el-border-color);
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
  transition: var(--el-transition-duration-fast);
}

.avatar-uploader .el-upload:hover {
  border-color: var(--el-color-primary);
}

.el-icon.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 178px;
  height: 178px;
  text-align: center;
}
</style>
